package com.cjm.one.webmanager.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.result.Result;
import com.cjm.one.model.base.BaseDeleteMoreVo;
import com.cjm.one.model.base.PasswordDto;
import com.cjm.one.model.system.dto.SysUserInfoDto;
import com.cjm.one.model.system.dto.SysUserInfoFromDto;
import com.cjm.one.model.system.entity.SysUserInfo;
import com.cjm.one.model.system.entity.SysUserRole;
import com.cjm.one.model.system.vo.SysUserInfoVo;
import com.cjm.one.webmanager.service.system.SysUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/manager/system")
@Tag(name = "1-系统管理-用户信息")
public class SysUserInfoController {

    @Resource
    private SysUserInfoService sysUserInfoService;
    @GetMapping("userInfo")
    @Operation(summary = "获取当前登录用户信息")
    public Result<SysUserInfo> userInfo(@RequestHeader("Authorization") String token){
        return Result.ok(sysUserInfoService.getLoginUser(token));
    }
    /**
     * @description: 根据id查询用户信息
     * @author: cjm
     * @date: 2024/9/30 21:11
     * @param:
     * @return:
     **/
    @GetMapping("/sysUser/{id}")
    @Operation(summary = "根据id查询用户信息")
    public Result<SysUserInfoVo> getSysUserById(@PathVariable("id") Long id) {
        log.info("uid:{}", id);
        SysUserInfoVo sysUserInfoVo =  sysUserInfoService.getSysUserById(id);
        return Result.ok(sysUserInfoVo);
    }
    @PostMapping("/sysUser")
    @Operation(summary = "新增用户信息")
    public Result<Boolean> addSysUser(@RequestBody SysUserInfo sysUserInfo) {
        log.info("新增用户信息:{}", sysUserInfo);
        return Result.ok(sysUserInfoService.addSysUser(sysUserInfo));
    }


    @DeleteMapping("/sysUser/{id}")
    @Operation(summary = "根据id删除系统用户")
    public Result<Boolean> deleteSysUser(@PathVariable("id") Long id) {
        log.info("根据id删除系统用户:{}", id);
        return Result.ok(sysUserInfoService.deleteSysUser(id));
    }
    @PutMapping("/sysUser")
    @Operation(summary = "根据id修改系统用户")
    public Result<Boolean> updateSysUser(@RequestBody SysUserInfoDto sysUserInfoDto) {
        log.info("根据id修改系统用户:{}", sysUserInfoDto);
        return Result.ok(sysUserInfoService.updateSysUser(sysUserInfoDto));
    }
    @PutMapping("/sysUser/password")
    @Operation(summary = "根据id修改系统用户密码")
    public Result<Boolean> updateSysUserPassword(@RequestBody PasswordDto passwordDto){
        log.info("根据id修改系统用户密码:{}", passwordDto);
        return Result.ok(sysUserInfoService.updateSysUserPassword(passwordDto));
    }

    @PutMapping("/sysUser/status")
    @Operation(summary = "根据id修改系统用户状态")
    public Result<Boolean> updateSysUserStatus(@RequestBody SysUserInfoDto sysUserInfoDto){
        log.info("根据id修改系统用户状态:{}", sysUserInfoDto);
        return Result.ok(sysUserInfoService.updateSysUserStatus(sysUserInfoDto));
    }
    @GetMapping("/sysUser/roleByUserId/{userId}")
    @Operation(summary = "根据用户id获取角色list")
    public Result<List<SysUserRole>> getSysUserRole(@PathVariable("userId") Long userId) {
        log.info("根据用户id获取角色list:{}", userId);
        return Result.ok(sysUserInfoService.getSysUserRole(userId));
    }

    @Operation(summary = "用户分页")
    @PostMapping("/sysUser/page")
    public Result<Page<SysUserInfoVo>> getSysUserPage(@RequestBody SysUserInfoFromDto queryVo) {
        log.info("用户分页:{}", queryVo);
        return Result.ok(sysUserInfoService.getSysUserPage(queryVo));
    }
    @Operation(summary = "批量删除用户")
    @DeleteMapping("/sysUser/deleteMore")
    //删除成功的条数
    public Result<BaseDeleteMoreVo> deleteSysUserMore(@RequestBody List<Long> ids){
        return Result.ok(sysUserInfoService.deleteSysUserMore(ids));
    }


    @PutMapping("/sysUser/resetPassword")
    @Operation(summary = "重置密码")
    public Result<Boolean> resetPassword(@RequestBody PasswordDto passwordDto){
        log.info("重置密码:{}", passwordDto);
        return Result.ok(sysUserInfoService.resetPassword(passwordDto));
    }

}
